import login from '../../api/login.js'
import personal from '../../api/personal.js'
import toast from '../../utils/toast.js'
const values = {
	mobile: '',
	verification: '',
	password: '',
	queryPassword: '',
	pay_pwd: '',
	invite_code:'',
}
const authenValues = {
	username: '',
	bank_card: '',
	bank_name: '',
	bank_mobile: '',
}
const phoneValues = {
	account: '',
	captcha: ''
}
const passwordValues = {
	account: '',
	password: ''
}
const mobile = [{
		required: true,
		message: '请输入手机号',
		trigger: 'blur,change'
	},
	{
		pattern: /^1[0-9]{10}$/g,
		// 正则检验前先将值转为字符串
		transform(value) {
			return String(value);
		},
		message: '请输入正确的手机号'
	},
]
const password = [{
	required: true,
	message: '请输入密码',
	trigger: 'blur,change'
}, ]
const state = {
	token: '',
	userinfo: {},
	fields: [{
			label: '手机号',
			prop: 'mobile',
			type: 'number',
			
			labelWidth: '160',
			placeholder: '请输入手机号',
		},
		{
			label: '验证码',
			prop: 'verification',
			type: 'number',
			labelWidth: '160',
			checked: true,
			placeholder: '请输入验证码',
		},
		/* 
		{
			label: '身份证号',
			prop: 'idcard',
			type: 'input',
			labelWidth: '160',
			placeholder: '请输入身份证号',
		},
		 */
		{
			label: '登录密码',
			prop: 'password',
			type: 'password',
			labelWidth: '160',
			placeholder: '请输入登录密码',
		},
		{
			label: '再次确认',
			prop: 'queryPassword',
			type: 'password',
			labelWidth: '160',
			placeholder: '请再次输入密码',
		},
		{
			label: '交易密码',
			prop: 'pay_pwd',
			type: 'password',
			labelWidth: '160',
			placeholder: '请输入交易密码',
		},
		{
			label: '邀请码',
			prop: 'invite_code',
			type: 'input',
			labelWidth: '160',
			placeholder: '请填写邀请码',
		},
	],
	rules: {
		mobile: mobile,
		password: password,
		/* 
		idcard: [
			{
				required: true,
				message: '请输入身份证号',
				trigger: 'blur,change'
			},
			{
				pattern: /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/g,
				message: '请输入正确的身份证号'
			}
		],
		 */
		queryPassword: [{
			required: true,
			message: '请输入确认密码',
			trigger: 'blur,change'
		}, ],
		pay_pwd: [{
				required: true,
				message: '请输入6位数交易密码',
				trigger: 'blur,change'
			},
			{
				min: 6,
				max:6,
				message: '请输入6位数交易密码',
				trigger: 'change'
			}
		],
		verification:[
			{
				required: true,
				message: '请输入4位短信验证码',
				trigger: 'blur,change'
			},
			{
				min: 4,
				max:4,
				message: '请输入4位短信验证码',
				trigger: 'change'
			}
		],
		invite_code: [
			{required: true, message: '请填写邀请码', trigger: 'blur,change'}
		]
	},
	values: JSON.parse(JSON.stringify(values)),
	authenFields: [{
			label: '姓名',
			prop: 'username',
			type: 'input',
			labelWidth: '180',
			placeholder: '请输入姓名',
		},
		{
			label: '身份证号',
			prop: 'idcard',
			type: 'input',
			labelWidth: '160',
			placeholder: '请输入身份证号',
		},
		{
			label: '银行卡号',
			prop: 'bank_card',
			type: 'number',
			labelWidth: '180',
			placeholder: '请输入银行卡号',
		},
		{
			label: '所属银行',
			prop: 'bank_name',
			type: 'select',
			labelWidth: '180',
			show: false,
			placeholder: '请选择所属银行',
		},
		{
			label: '预留手机号',
			prop: 'bank_mobile',
			type: 'number',
			labelWidth: '180',
			placeholder: '请输入预留手机号',
		},
	],
	authenRules: {
		username: [{
			required: true,
			message: '请输入姓名',
			trigger: 'blur,change'
		}],
		idcard: [
			{
				required: true,
				message: '请输入身份证号',
				trigger: 'blur,change'
			},
			{
				pattern: /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/g,
				message: '请输入正确的身份证号'
			}
		],
		bank_card: [{
				required: true,
				message: '请输入银行卡号',
				trigger: 'blur,change'
			},
			// {
			// 	pattern:  /^([1-9]{1})(\d{14}|\d{18})$/g,
			// 	// 正则检验前先将值转为字符串
			// 	transform(value) {
			// 		return String(value);
			// 	},
			// 	message: '请输入正确的银行卡号'
			// },
		],
		bank_name: [{
			required: true,
			message: '请选择所属银行',
			trigger: 'blur,change'
		}],
		bank_mobile: mobile,
	},
	authenValues: JSON.parse(JSON.stringify(authenValues)),
	actionSheetList: [],
	phoneFields: [{
			label: '手机号',
			prop: 'account',
			type: 'number',
			labelWidth: '160',
			leftIcon: '/static/images2/login/icon-zh.png',
			placeholder: '请输入手机号',
		},
		{
			label: '验证码',
			prop: 'captcha',
			type: 'number',
			labelWidth: '160',
			checked: true,
			leftIcon: '/static/images2/login/icon-mm.png',
			placeholder: '请输入验证码',
		},
	],
	passwordFields: [{
			label: '手机号',
			prop: 'account',
			type: 'number',
			labelWidth: '160',
			leftIcon: '/static/images2/login/icon-mm.png',
			placeholder: '请输入手机号',
		},
		{
			label: '密码',
			prop: 'password',
			type: 'password',
			labelWidth: '160',
			leftIcon: '/static/images2/login/icon-zh.png',
			placeholder: '请输入密码',
		},
	],
	phoneValues: JSON.parse(JSON.stringify(phoneValues)),
	passwordValues: JSON.parse(JSON.stringify(passwordValues)),
	passwordRules: {
		account: mobile,
		password: [{
			required: true,
			message: '请输入密码',
			trigger: 'blur,change'
		}, ]
	}

}
const mutations = {
	//重置验证码表单
	RESET_PHONE(state) {
		state.phoneValues = JSON.parse(JSON.stringify(phoneValues))
	},
	//重置验证码表单
	RESET_PASSWORD(state) {
		state.passwordValues = JSON.parse(JSON.stringify(passwordValues))
	}
}
const actions = {
	//债务人注册账号
	REGISTER_LOGIN({
		state
	}) {
		login.registerPost(state.values).then(res => {
			if (res) {
				toast.success('注册成功')
				state.values = JSON.parse(JSON.stringify(values))
				uni.setStorageSync('token', res.data.userinfo.token);
				uni.navigateTo({
					url: '/pages/loanee/login/authentication'
				});
			}
		})
	},
	//债务人注册完成认证
	AUTHENTICATION({
		state
	}) {
		login.profilePost(state.authenValues).then(res => {
			if (res) {
				state.authenValues = JSON.parse(JSON.stringify(authenValues))
				toast.success('提交完成')
				uni.navigateTo({
					url: '/pages/loanee/login/waitFor'
				});
			}
		})
	},
	//获取银行列表
	GET_BANK_DATA({
		state
	}) {
		login.bankListGet().then(res => {
			res.data.map(item => {
				state.actionSheetList.push({
					label: item,
					value: item
				})
			})
		})
	},
	//债务人登录
	OBLIGOR_ASHORE({
		state
	}, num) {
		let value = {}
		if (num == 1) {
			value = state.phoneValues
			login.mobilelogin(value).then(res => {
				if (res) {
					state.token = res.data.userinfo.token
					state.phoneValues = JSON.parse(JSON.stringify(phoneValues))
					state.passwordValues = JSON.parse(JSON.stringify(passwordValues))
					uni.setStorageSync('token', res.data.userinfo.token);
					if (res.data.userinfo.bank_status == '0') {
						uni.setStorageSync('token', res.data.userinfo.token);
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: '您还没有认证个人信息',
							success: function(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/loanee/login/authentication'
									});
								}
							}
						});
					} else {
						if (res.data.userinfo.check_status == '0') {
							uni.navigateTo({
								url: '/pages/loanee/login/waitFor'
							});
						} else if (res.data.userinfo.check_status == '1') {
							toast.successTab('登录成功','/pages/loanee/obligor/index')
							uni.setStorageSync('token', res.data.userinfo.token);
							uni.setStorageSync('group_id', res.data.userinfo.group_id);
							uni.setStorageSync('userinfo', res.data.userinfo);
						
						} else {
							toast.error('登录失败，请联系相关人员')
						}
					}
				}
			})
		} else {
			value = state.passwordValues
			login.obligorLogin(value).then(res => {
				if (res) {
					state.token = res.data.userinfo.token
			
					state.phoneValues = JSON.parse(JSON.stringify(phoneValues))
					state.passwordValues = JSON.parse(JSON.stringify(passwordValues))
	uni.setStorageSync('token', res.data.userinfo.token);
					if (res.data.userinfo.bank_status == '0') {
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: '您还没有认证个人信息',
							success: function(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/loanee/login/authentication'
									});
								}
							}
						});
					} else {
						if (res.data.userinfo.check_status == '0') {
							uni.navigateTo({
								url: '/pages/loanee/login/waitFor'
							});
						} else if (res.data.userinfo.check_status == '1') {
							toast.successTab('登录成功','/pages/loanee/obligor/index')
						    uni.setStorageSync('token', res.data.userinfo.token);
						    uni.setStorageSync('group_id', res.data.userinfo.group_id);
						    uni.setStorageSync('userinfo', res.data.userinfo);
						} else {
							toast.error('登录失败，请联系相关人员')
						}
					}
				}
			})
		}
		// uni.showToast({
		//     title: '标题17',
		//     duration: 2000
		// });
		
	},
	//债权人登录
	CREDITOR_ASHORE({
		state
	}) {
		login.creditorLogin(state.passwordValues).then(res => {
			if (res) {
				toast.successTab('登录成功','/pages/creditor/personam/index')
				uni.setStorageSync('token', res.data.userinfo.token);
				uni.setStorageSync('group_id', res.data.userinfo.group_id);
				uni.setStorageSync('userinfo', res.data.userinfo);
			}
		})
	},
	IDENTIFYING_CODE({state},data){
   return  login.sendPost(data)
	},
	GET_STATUS({state}){
		personal.bankInfo().then(res=>{
			if(res){
             if(res.data.check_status=='1'){
				uni.navigateTo({
					url: '/pages/loanee/login/index'
				});
			 }
			}
		})
	}
}
export default {
	namespaced: true,
	state,
	mutations,
	actions
}
